package com.hkbigdata.datagovernance.governance.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hkbigdata.datagovernance.governance.bean.GovernanceAssessTable;
import com.hkbigdata.datagovernance.governance.bean.GovernanceType;
import com.hkbigdata.datagovernance.governance.mapper.GovernanceAssessTableMapper;
import com.hkbigdata.datagovernance.governance.service.GovernanceAssessTableService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkbigdata.datagovernance.governance.service.GovernanceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 表治理考评情况 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-01-10
 */
@Service
@DS("mysqlWriting")
public class GovernanceAssessTableServiceImpl extends ServiceImpl<GovernanceAssessTableMapper, GovernanceAssessTable> implements GovernanceAssessTableService {

    @Autowired
    GovernanceAssessTableMapper governanceAssessTableMapper;

    @Autowired
    GovernanceTypeService governanceTypeService;

    @Override
    public void calcGovernanceAssessTable(String assessDate) {
//        每次重跑之前删除当天数据
        remove(new QueryWrapper<GovernanceAssessTable>().eq("assess_date", assessDate));


        List<GovernanceAssessTable> governanceAssessTableList = governanceAssessTableMapper
                .selectGovernanceAssessTable(assessDate);

//        获取到每个模块的权重
        List<GovernanceType> governanceTypeList = governanceTypeService.list();
//        创建map存放每个模块权重 k:type_code v:score
        HashMap<String, BigDecimal> codeToScoreMap = new HashMap<>();
        for (GovernanceType governanceType : governanceTypeList) {
            codeToScoreMap.put(governanceType.getTypeCode(), governanceType.getTypeWeight());
        }

//        根据不同权重计算分数
        for (GovernanceAssessTable governanceAssessTable : governanceAssessTableList) {
            BigDecimal calc = (governanceAssessTable.getScoreCalcAvg() == null?BigDecimal.valueOf(100)
                    : governanceAssessTable.getScoreCalcAvg())
                    .multiply(codeToScoreMap.get("CALC")).setScale(2, RoundingMode.HALF_UP);

            BigDecimal spec = (governanceAssessTable.getScoreSpecAvg() == null?BigDecimal.valueOf(100)
                    : governanceAssessTable.getScoreSpecAvg())
                    .multiply(codeToScoreMap.get("SPEC")).setScale(2, RoundingMode.HALF_UP);

            BigDecimal storage = (governanceAssessTable.getScoreStorageAvg() == null?BigDecimal.valueOf(100)
                    : governanceAssessTable.getScoreStorageAvg())
                    .multiply(codeToScoreMap.get("STORAGE")).setScale(2, RoundingMode.HALF_UP);

            BigDecimal security = (governanceAssessTable.getScoreSecurityAvg() == null?BigDecimal.valueOf(100)
                    : governanceAssessTable.getScoreSecurityAvg())
                    .multiply(codeToScoreMap.get("SECURITY")).setScale(2, RoundingMode.HALF_UP);

            BigDecimal quality = (governanceAssessTable.getScoreQualityAvg() == null?BigDecimal.valueOf(100)
                    : governanceAssessTable.getScoreQualityAvg())
                    .multiply(codeToScoreMap.get("QUALITY")).setScale(2, RoundingMode.HALF_UP);

            BigDecimal score = calc.add(spec).add(quality).add(storage).add(security);
//            设置分数和创建时间
            governanceAssessTable.setScoreOnTypeWeight(score);
            governanceAssessTable.setCreateTime(new Date());
        }
//        保存到mysql
        saveBatch(governanceAssessTableList);
    }
}
